package com.pos.uniplugin_all;

import android.app.Activity;
import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.pos.uniplugin_all.dhscale.WebSocketUtil;

import io.dcloud.feature.uniapp.annotation.UniJSMethod;
import io.dcloud.feature.uniapp.bridge.UniJSCallback;
import io.dcloud.feature.uniapp.common.UniDestroyableModule;
import io.dcloud.feature.uniapp.utils.UniResourceUtils;

public class ElectronicScaleMoudle extends UniDestroyableModule {
    public String IP = "ip";
    public String PORT  = "port";
    @UniJSMethod(uiThread = false)
    public void connectES(JSONObject options, UniJSCallback jsCallback){
        if (mUniSDKInstance.getContext() instanceof Activity) {
            String ip = options.getString(IP);
            String port = options.getString(PORT);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        String conncetStr = WebSocketUtil.main(ip, port);
                        JSONObject result = new JSONObject();
                        result.put("result", "连接情况： " + conncetStr);
                        jsCallback.invokeAndKeepAlive(result);
                    }catch (Exception e){
                        Log.d("wdx", "错误： " + e.getMessage());
                        JSONObject result = new JSONObject();
                        result.put("result", "错误： " + e.getMessage());
                        jsCallback.invokeAndKeepAlive(result);
//                        Looper.prepare();
//                        Toast.makeText(MainActivity.this, "错误： " + e.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }
            }).start();
        }
    }




    @Override
    public void destroy() {

    }
}
